package Demo2_11;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User：Lenovo
 * Date:2025-02-11
 * Time:11:47
 */
//自己实现队列
public class MyStack {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE=10;
    public MyStack(){
        elem=new int[DEFAULT_SIZE];
    }
    //存储数据
    public void push(int val){
        if(isFull()) {
            //扩容
            grow();
        }
            elem[usedSize]=val;
            usedSize++;
    }
    private void grow(){
        elem= Arrays.copyOf(elem,elem.length);
    }
    public boolean isFull(){
        return usedSize==elem.length;
    }
    //删除数据
    public int pop(){
        if(isEmpty()){
            throw new StackEmptyException("栈为空异常");
        }
        usedSize--;
        return elem[usedSize];
    }
    public boolean isEmpty(){
        return usedSize==0;
    }
    //获取栈顶元素，但是不删除当前元素
    public int peek(){
        if(isEmpty()){
            throw new StackEmptyException("栈为空异常");
        }
        return elem[usedSize-1];
    }
    public int size(){
        return usedSize;
    }

    public static void main(String[] args) {
        MyStack stack=new MyStack();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);
        int val=stack.pop();//删除
        System.out.println(val);//45
        val=stack.pop();
        System.out.println(val);//34
        int val2=stack.peek();//只获取不删除
        System.out.println(val2);//23
        val2=stack.peek();//只获取不删除
        System.out.println(val2);//23
        boolean empty=stack.isEmpty();
        System.out.println(empty);//false
    }
}
